package de.devisnik.eidle;

import java.util.ArrayList;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import de.devisnik.eidle.screen.IScreen;

/**
 * The activator class controls the plug-in life cycle
 */
public class EidleActivator extends AbstractUIPlugin {

	private static final String SCREENS_EXTENSION_POINT = "de.devisnik.eidle.screens";

	public static final String SPLASH_IMAGE_KEY = "splash";

	// The plug-in ID
	public static final String PLUGIN_ID = "de.devisnik.eidle";

	// The shared instance
	private static EidleActivator plugin;

	private IScreen[] itsScreens;

	/**
	 * The constructor
	 */
	public EidleActivator() {
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(final BundleContext context) throws Exception {
		super.start(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(final BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	protected void initializeImageRegistry(final ImageRegistry reg) {
		reg.put(SPLASH_IMAGE_KEY, imageDescriptorFromPlugin(PLUGIN_ID,
				"splash.bmp"));
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static EidleActivator getDefault() {
		return plugin;
	}

	public IScreen[] getScreens() {
		if (itsScreens == null) {
			final IExtensionPoint extensionPoint = Platform
					.getExtensionRegistry().getExtensionPoint(
							SCREENS_EXTENSION_POINT);
			final IExtension[] extensions = extensionPoint.getExtensions();
			final ArrayList screenList = new ArrayList/* <IScreen> */();
			for (int i = 0; i < extensions.length; i++) {
				final IConfigurationElement[] configurationElements = extensions[i]
						.getConfigurationElements();
				for (int j = 0; j < configurationElements.length; j++) {
					try {
						final IConfigurationElement configurationElement = configurationElements[j];
						final IScreen screen = (IScreen) configurationElement
								.createExecutableExtension("class");
						screenList.add(screen);
						EidlePreferences.setScreenDefaultEnablement(screen,
								true);
					} catch (final CoreException e) {
						e.printStackTrace();
					}
				}
			}
			logScreens(screenList);

		}
		return itsScreens;
	}

	private void logScreens(final ArrayList screenList) {
		itsScreens = (IScreen[]) screenList.toArray(new IScreen[screenList
				.size()]);
		final StringBuffer logMessage = new StringBuffer();
		logMessage.append(screenList.size());
		logMessage.append(" screens found");
		logMessage.append(" [");
		String separator = "";
		for (int i = 0; i < itsScreens.length; i++) {
			final IScreen screen = itsScreens[i];
			logMessage.append(separator);
			logMessage.append(screen.getName());
			separator = ",";
		}
		logMessage.append("]");
		log(logMessage.toString());
	}

	public String getDataLocation() {
		final String rawDataDir = Platform.getInstallLocation().getURL()
				.getFile().substring(1)
				+ "/data";
		final String dataDir = new Path(rawDataDir).toOSString();
		return dataDir;
	}

	public void log(final String message) {
		final Status status = new Status(IStatus.INFO, PLUGIN_ID, message);
		getLog().log(status);
	}
}
